package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {

        return roleDao.findAll();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);


        Page<CheckGroup> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }


    @Override
    public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setRoleAndMenu(menuIds,roleId);
        this.setRoleAndPermission(permissionIds,roleId);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findMenuIdsByUserId(Integer id) {
        return roleDao.findMenuIdsByUserId(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public void edit(Integer[] menuIds, Integer[] permissionIds, Role role) {
        //修改检查主基本信息,操作检查组t_user表
        roleDao.edit(role);
        //清理房钱检查组关联的检查项,操作中间关系表t_user_role表
        roleDao.deleteAssocication4Menu(role.getId());
        roleDao.deleteAssocication4Permission(role.getId());


        //重新建立当前关联关系
        Integer userId = role.getId();
        setRoleAndMenu(menuIds, userId);
        setRoleAndPermission(permissionIds, userId);

    }

    @Override
    public void deleteById(Integer id) {
        long count = roleDao.findCountRoleId(id);

        if (count > 0) {
            throw new RuntimeException("当前角色被引用，不能删除");
        }
        roleDao.deleteAssocication1(id);
        roleDao.deleteAssocication2(id);
        roleDao.deleteById(id);

    }

    private void setRoleAndMenu(Integer[] menuIds, Integer roleId) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
    private void setRoleAndPermission(Integer[] permissionIds, Integer roleId) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
}
